#!/usr/bin/perl

print "Processing CSV Data\n";
my $file=$ARGV[0];
print "$file\n";
open(INFO,$file);
my @lines = <INFO>;
close(INFO);

my $stationId = (split/\./,$file)[0];

$lines[0] =~ s/\s//g;
my @headerList = split /,/, $lines[0];
my @pollutants = @headerList[2..$#headerList];

my @measures=@lines[1..$#lines];

my $outFileName = ${stationId}."_full.csv";
print "$outFileName\n";
open(OUTDATA,">$outFileName");

foreach $measure (@measures) {
    chomp($measure);
    $measure =~ s/\s//g;
    my @curline = split /,/, $measure;
    my @curDate = split /\//, $curline[0];
    my $curTime=$curline[1]-1;
    my $curDateTime="$curDate[2]-$curDate[0]-$curDate[1] $curTime:00";
    for ($i = 0; $i < @pollutants; $i++) {
       my $value = ($curline[$i+2] == "N")? "\\N" : $curline[$i+2];
        print OUTDATA "$stationId,$curDateTime,$pollutants[$i],$value\n";
    }
}

close(OUTDATA);
